<?php
namespace Gd11x5\Controller;
use Think\Controller;

//引入阿里大于短信api
use Aliyun\Core\Config;
use Aliyun\Core\Profile\DefaultProfile;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;

class SmsController extends Controller {
	
	/**
	 * 数据处理
	 */
	public function send_message(){
		
// 		return $data='发送成功';
// 		die();
		$phone=I("post.phone");
		$ip = get_real_ip ();
		//查找是否已经注册
		$user = D('Agent') -> where("phone = {$phone}") -> find();
		if ($user) {
			echo 2;
		}else{
			if($this->checkCan($phone,$ip)){
				$this->send_phone($phone);
			}else{
				echo 3;//发送太频繁
			}		
		}
	}
	
	/**
	 * 发送验证码验证
	 */
	public function drawings_send_message(){
		$phone=I("post.phone");
		$ip = get_real_ip ();
		//查找是否已经注册
		$user = D('Agent') -> where("phone = {$phone}") -> find();
		if ($user) {
			if($this->checkCan($phone,$ip)){
				$this->send_phone($phone);
			}else{
				echo 3;//发送太频繁
			}
		}else{
			echo 2;
		}
	}
	
	/**
	 * 忘记密码发送验证码验证
	 */
	public function forget_send_message(){
		$phone=I("post.phone");
		$username=I("post.username");
		//查找是否已经注册
		$where_find['phone']=$phone;
		$where_find['username']=$username;
		$user = D('Agent') -> where($where_find) -> find();
		if ($user) {
			$ip = get_real_ip ();
			if($this->checkCan($phone,$ip)){
				$this->send_phone($phone);
			}else{
				echo 3;//发送太频繁
			}
		}else{
			echo 2;//账号绑定的手机号有误!
		}
	}
	
	
	/**
	 * 生成短信验证码
	 * @param  integer $length [验证码长度]
	 */
	public function createSMSCode($length = 4){
		$min = pow(10 , ($length - 1));
		$max = pow(10, $length) - 1;
		return rand($min, $max);
	}
	
	
	/**
	 * 发送验证码
	 * @param  [integer] $phone [手机号]
	 */
	public function send_phone($phone){
		$code=$this->createSMSCode($length = 4);
	
		require_once  './Api/dysms/vendor/autoload.php';    //此处为你放置API的路径
		Config::load();             //加载区域结点配置
	
		$accessKeyId = 'LTAI4gZzGdtAMRRn'; //申请阿里云的accessKeyId
		$accessKeySecret = 'pMCb7InFzsC7r19bU2NDOLX2Ul8glK'; //申请阿里云的accessKeySecret
		$templateCode = 'SMS_98920047';   //短信模板ID
	
		//短信API产品名（短信产品名固定，无需修改）
		$product = "Dysmsapi";
	
		//短信API产品域名（接口地址固定，无需修改）
		$domain = "dysmsapi.aliyuncs.com";
	
		//暂时不支持多Region（目前仅支持cn-hangzhou请勿修改）
		$region = "cn-hangzhou";
	
		// 初始化用户Profile实例
		$profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
	
		// 增加服务结点
		DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", $product, $domain);
	
		// 初始化AcsClient用于发起请求
		$acsClient = new DefaultAcsClient($profile);
	
		// 初始化SendSmsRequest实例用于设置发送短信的参数
		$request = new SendSmsRequest();
	
		// 必填，设置短信接收号码
		$request->setPhoneNumbers($phone);
	
		// 必填，设置签名名称
		$request->setSignName("唯诺科技");
	
		// 必填，设置模板CODE
		$request->setTemplateCode("SMS_98920047");
	
		$smsData = array('number'=>$code);    //所使用的模板若有变量 在这里填入变量的值  我的变量名为username此处也为username
	
		//选填-假如模板中存在变量需要替换则为必填(JSON格式),友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败
		$request->setTemplateParam(json_encode($smsData));
	
		//发起访问请求
		$acsResponse = $acsClient -> getAcsResponse($request);
		//返回请求结果
		$result = json_decode(json_encode($acsResponse), true);
		$resp = $result['Code'];
		$this->sendMsgResult($resp,$phone,$code);
	}
	
	
	/**
	 * 验证手机号是否发送成功  前端用ajax，发送成功则提示倒计时，如50秒后可以重新发送
	 * @param  [json] $resp  [发送结果]
	 * @param  [type] $phone [手机号]
	 * @param  [type] $code  [验证码]
	 * @return [type]        [description]
	 */
	private function sendMsgResult($resp,$phone,$code){
		
		if ($resp == "OK") {
			$data['phone']=$phone;
			$data['code']=$code;
			$data['send_time']=time();
			$data['ip']=get_real_ip();
			$result=D("Smsverif")->add($data);
			if($result){
				$data="发送成功";
			}else{
				$data="发送失败";
			}
		} else{
			$data="发送失败";
		}
		return $data;
	}
	
	
	/**
	 * 验证短信验证码是否有效,前端用jquery validate的remote
	 * @return [type] [description]
	 */
	public function checkSMSCode(){
		$phone = $_POST['phone'];
		$code = $_POST['verify'];
		$nowTimeStr = time();
		$smscodeObj = D("Smsverif")->where("phone={$phone} and code = {$code}")->find();
		if($smscodeObj){
			$smsCodeTimeStr = $smscodeObj['send_time'];
			$recordCode = $smscodeObj['code'];
			$flag = $this->checkTime($nowTimeStr, $smsCodeTimeStr);
			if($flag!=true || $code !== $recordCode){
				echo 'no';
			}else{
				echo 'ok';
			}
		}
	}
	
	
	/**
	 * 验证短信验证码是否有效,前端用jquery validate的remote
	 * @return [type] [description]
	 */
	public function checkCode($phone,$code){
		$nowTimeStr = time();
		$where['phone']=$phone;
		$where['code']=$code;
		$smscodeObj = D("Smsverif")->where($where)->find();
		if($smscodeObj){
			$smsCodeTimeStr = $smscodeObj['send_time'];
			$recordCode = $smscodeObj['code'];
			$flag = $this->checkTime($nowTimeStr, $smsCodeTimeStr);
			if($flag!=true || $code !== $recordCode){
				return false;
			}else{
				return true;
			}
		}
	}
	
	
	/**
	 * 验证验证码是否在可用时间
	 *  @param  [json] $nowTimeStr  [发送结果]
	 * @param  [type] $smsCodeTimeStr [手机号]
	 */
	public function checkTime ($nowTimeStr,$smsCodeTimeStr) {
		$time = $nowTimeStr - $smsCodeTimeStr;
		if ($time>900) {
			return false;
		}else{
			return true;
		}
	}
	
	/**
	 * 验证验证码发送是否允许//1天一个号码或一个ip最多发送10条
	 *  @param  [json] $nowTimeStr  [发送结果]
	 * @param  [type] $smsCodeTimeStr [手机号]
	 */
	public function checkCan($phone,$ip){
		
		$where1['phone']=array('eq',$phone);
		$where1['send_time']=array('gt',strtotime("-1 day"));
		$where2['ip']=array('eq',$ip);
		$where_main['_complex'] = array(
				$where1,
				$where2,
				'_logic' => 'or'
		);
		$result = D("Smsverif")->where($where_main)->count();
		if($result>10){
			return false;
		}else{
			return true;
		}
		
		
	}
}